package com.ruoyi.buss.dxts.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.exception.CustomException;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.buss.dxts.domain.SmSendRecord;
import com.ruoyi.buss.dxts.service.ISmSendRecordService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 用户登录相关短信Controller
 * 
 * @author zjh
 * @date 2021-08-02
 */
@RestController
@RequestMapping("/smSendRecord")
public class SmSendRecordController extends BaseController
{
    @Autowired
    private ISmSendRecordService smSendRecordService;

    /**
     * 查询用户登录相关短信列表
     */
    @PreAuthorize(hasPermi = "system:smSendRecord:list")
    @GetMapping("/list")
    public TableDataInfo list(SmSendRecord smSendRecord)
    {
        startPage();
        List<SmSendRecord> list = smSendRecordService.selectSmSendRecordList(smSendRecord);
        return getDataTable(list);
    }

    /**
     * 校验短信验证码
     * 验证码不需要验证权限
     */
    @GetMapping("/vaildSm")
    public AjaxResult vaildSm(SmSendRecord smSendRecord)
    {
        SmSendRecord tmp = smSendRecordService.vaildSm(smSendRecord);
        if (tmp != null){
            return AjaxResult.success(true);
        }  else {
            return AjaxResult.success(false);
        }
    }

    /**
     * 导出用户登录相关短信列表
     */
    @PreAuthorize(hasPermi = "system:smSendRecord:export")
    @Log(title = "用户登录相关短信", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SmSendRecord smSendRecord) throws IOException
    {
        List<SmSendRecord> list = smSendRecordService.selectSmSendRecordList(smSendRecord);
        ExcelUtil<SmSendRecord> util = new ExcelUtil<SmSendRecord>(SmSendRecord.class);
        util.exportExcel(response, list, "用户登录相关短信数据");
    }

    /**
     * 获取用户登录相关短信详细信息
     */
    @PreAuthorize(hasPermi = "system:smSendRecord:query")
    @GetMapping(value = "/{messageId}")
    public AjaxResult getInfo(@PathVariable("messageId") String messageId)
    {
        return AjaxResult.success(smSendRecordService.selectSmSendRecordById(messageId));
    }

    /**
     * 新增用户登录相关短信
     */
    @PreAuthorize(hasPermi = "system:smSendRecord:add")
    @Log(title = "用户登录相关短信", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SmSendRecord smSendRecord)
    {
        return toAjax(smSendRecordService.insertSmSendRecord(smSendRecord));
    }

    /**
     * 修改用户登录相关短信
     */
    @PreAuthorize(hasPermi = "system:smSendRecord:edit")
    @Log(title = "用户登录相关短信", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SmSendRecord smSendRecord)
    {
        return toAjax(smSendRecordService.updateSmSendRecord(smSendRecord));
    }

    /**
     * 删除用户登录相关短信
     */
    @PreAuthorize(hasPermi = "system:smSendRecord:remove")
    @Log(title = "用户登录相关短信", businessType = BusinessType.DELETE)
	@DeleteMapping("/{messageIds}")
    public AjaxResult remove(@PathVariable String[] messageIds)
    {
        return toAjax(smSendRecordService.deleteSmSendRecordByIds(messageIds));
    }

//    @PreAuthorize(hasPermi = "system:smSendRecord:smsSend")
    @PostMapping("/smsSend")
    public AjaxResult smsSend(@RequestBody SmSendRecord smSendRecord)
    {
        if (StringUtils.isEmpty(smSendRecord.getPhone())) {
            throw new CustomException("参数错误,手机号码不能为空");
        }
//        if (StringUtils.isEmpty(smSendRecord.getUserId())) {
//            throw new CustomException("参数错误,员工号不能为空");
//        }
        if (StringUtils.isEmpty(smSendRecord.getSmType())) {
            throw new CustomException("参数错误,smType不存在(0 登录 1 修改密码 2 更换设备)");
        }
        return AjaxResult.success(smSendRecordService.smsSend(smSendRecord));
    }
}
